package vno.sci.mapEditor
{
	import flash.globalization.Collator;
	import flash.utils.ByteArray;
	
	import system.Char;
	
	import vno.sci.PinyinNumericSort;
	import vno.sci.Util;

	/**
	 * x,y是建筑物坐标，s是建筑物的描述 
	 * 继承了ExPoint
	 * 最开始由ByteArray生成Vector.<BuildingInfo>, BuildingInfo就是点-描述这样的数据对。
	 * 然后用sci.Util.init函数接受这个Vector, 它会对它进行两次以x或y为基准的排序
	 * sci.Util.showTest返回一个Vector.<IPoint>在一个矩形区域内的Vector.<IPoint>
	 * 最后localMarkerToStage产生一个Vector.<BuildingInfo>的副本，假设参数是以大矩阵的相对坐标，返回的x,y是相对舞台的绝对坐标。
	 * @author scinart
	 */	
	public class BuildingInfo
	{
		private var _x:int;
		private var _y:int;
		private var _s:String;
		private var _assoc:Object = {};
		
		public function BuildingInfo(x_:int, y_:int, s_:String)
		{
			_x = x_;
			_y = y_
			_s = s_;
		}

		public function get y():int{return _y;}
		public function set y(value:int):void{_y = value;}

		public function get x():int{return _x;}
		public function set x(value:int):void{_x = value;}

		public function get s():String{return _s;}

		public function get assoc():Object{return _assoc;}
		
		public function  toString():String
		{
			return _s;
		}

		
		/**
		 * @param ba mapMarker生成的binary文件 
		 * @return [BuildingInfo, BuildingInfo ... BuildingInfo]
		 */		
		public static function markerToArray(ba:ByteArray):Vector.<BuildingInfo>
		{
			var len:int = ba.readUnsignedShort();
			var mW:int = ba.readUnsignedShort();
			var mH:int = ba.readUnsignedShort();
			var bW:int = ba.readUnsignedShort();
			var bH:int = ba.readUnsignedShort();
			
			var arr:Vector.<BuildingInfo> = new Vector.<BuildingInfo>(len,true);
			for (var ii:int = 0; ii < len; ii++)
			{
				arr[ii] = new BuildingInfo(ba.readUnsignedShort(),ba.readUnsignedShort(),ba.readUTF());
			}
			return arr;
		}
		
		public static function arrayToBinary(arr:Vector.<BuildingInfo>):ByteArray
		{
			var ba:ByteArray = new ByteArray;
			ba.writeShort(arr.length);
			ba.writeShort(0);
			ba.writeShort(0);
			ba.writeShort(0);
			ba.writeShort(0);
			
			for each (var item:BuildingInfo in arr)
			{
				ba.writeShort(item.x);
				ba.writeShort(item.y);
				ba.writeUTF(item.s);
			}
			
			ba.position = 0;
			return ba;
			
		}
		
		/**
		 * 
		 * @param visionX 可视矩形对于大矩形的localX
		 * @param visionY 同上
		 * @param v 保存相对于大矩阵的点-描述数组
		 * @return vec 保存相对于舞台的点-描述数组
		 * 
		 */		
		public static function localMarkerToStage(visionX:int, visionY:int, v:Vector.<BuildingInfo>):Vector.<BuildingInfo>
		{
			var vec:Vector.<BuildingInfo> = new Vector.<BuildingInfo>(v.length,true);
			for(var i:int = 0; i < v.length; i++)
			{
				vec[i] = new BuildingInfo(v[i].x-visionX, v[i].y - visionY, v[i].s);
			}
			return vec;
		}

		public static function appendAttributes(ba:ByteArray):ByteArray
		{
			var len:int = ba.readUnsignedShort();
			var mW:int = ba.readUnsignedShort();
			var mH:int = ba.readUnsignedShort();
			var bW:int = ba.readUnsignedShort();
			var bH:int = ba.readUnsignedShort();
			
			var arr:Vector.<BuildingInfo> = new Vector.<BuildingInfo>(len,true);
			for (var ii:int = 0; ii < len; ii++)
			{
				arr[ii] = new BuildingInfo(ba.readUnsignedShort(),ba.readUnsignedShort(),ba.readUTF());
			}
			
			var collator:Collator = new Collator("zh-Hant@collation=pinyin");
			arr.sort(function (a:BuildingInfo, b:BuildingInfo):int
			{
//				return collator.compare(a._s, b._s);
				return PinyinNumericSort.compare(a._s, b._s);
			});
			
			
			
			var a:Object = {};
			/*
			"地标" = 0
			"功能" = 1
			"家属楼" = 2
			"教学" = 3
			"食堂" = 4
			"宿舍" = 5
			"其他" = 6
			*/
			
			a["西区公寓"] = 5;
			a["一食堂"] = 4;
			a["二食堂"] = 4;
			a["清真食堂"] = 4;
			a["周总理像"] = 0;
			a["东门"] = 0;
			a["西南联大纪念碑"] = 0;
			a["校钟"] = 0;
			a["马蹄湖"] = 0;
			a["新开湖"] = 0;
			a["陈省身故居"] = 0;
			a["大榕树"] = 0;
			a["出版社"] = 1;
			a["购电室"] = 1;
			a["保卫处"] = 1;
			a["图书馆"] = 1;
			a["南开大学运动场"] = 1;
			a["篮球场"] = 1;
			a["网球场"] = 1;
			a["体育馆(老馆)"] = 1;
			a["小礼堂"] = 1;
			a["体育中心(新馆)"] = 1;
			a["行政楼"] = 1;
			a["老图书馆"] = 1;
			a["校医院"] = 1;
			a["学生活动中心"] = 1;
			a["静园"] = 1;
			a["离休干部管理处"] = 1;
			a["旧学生活动中心"] = 1;
			a["南开浴园"] = 1;
			a["66楼"] = 2;
			a["67楼"] = 2;
			a["68楼"] = 2;
			a["69楼"] = 2;
			a["50楼"] = 2;
			a["51楼"] = 2;
			a["52楼"] = 2;
			a["53楼"] = 2;
			a["54楼"] = 2;
			a["55楼"] = 2;
			a["56楼"] = 2;
			a["57楼"] = 2;
			a["58楼"] = 2;
			a["62楼"] = 2;
			a["61楼"] = 2;
			a["60楼"] = 2;
			a["59楼"] = 2;
			a["49楼"] = 2;
			a["48楼"] = 2;
			a["47楼"] = 2;
			a["46楼"] = 2;
			a["45楼"] = 2;
			a["35楼"] = 2;
			a["36楼"] = 2;
			a["31楼"] = 2;
			a["33楼"] = 2;
			a["32楼"] = 2;
			a["30楼"] = 2;
			a["21楼"] = 2;
			a["15楼"] = 2;
			a["13楼"] = 2;
			a["7楼"] = 2;
			a["8楼"] = 2;
			a["9楼"] = 2;
			a["10楼"] = 2;
			a["11楼"] = 2;
			a["12楼"] = 2;
			a["18楼"] = 2;
			a["17楼"] = 2;
			a["16楼"] = 2;
			a["14楼"] = 2;
			a["20斋"] = 2;
			a["21斋"] = 2;
			a["20楼"] = 2;
			a["19楼"] = 2;
			a["1楼"] = 2;
			a["2楼"] = 2;
			a["3楼"] = 2;
			a["6楼"] = 2;
			a["5楼"] = 2;
			a["4楼"] = 2;
			a["7楼"] = 2;
			a["8楼"] = 2;
			a["9楼"] = 2;
			a["11楼"] = 2;
			a["10楼"] = 2;
			a["12楼"] = 2;
			a["15楼"] = 2;
			a["17楼"] = 2;
			a["22楼"] = 2;
			a["21楼"] = 2;
			a["23楼"] = 2;
			a["24楼"] = 2;
			a["25楼"] = 2;
			a["18楼"] = 2;
			a["19楼"] = 2;
			a["14楼"] = 2;
			a["经院一教"] = 3;
			a["经院二教(圆阶)"] = 3;
			a["经济学院"] = 3;
			a["范孙楼"] = 3;
			a["电教中心（新阶）"] = 3;
			a["伯苓楼"] = 3;
			a["成教学院"] = 3;
			a["二主楼"] = 3;
			a["生命科学院(四教)"] = 3;
			a["六教"] = 3;
			a["五教"] = 3;
			a["主楼"] = 3;
			a["省身楼（数学研究中心）"] = 3;
			a["文科创新楼"] = 3;
			a["元素所楼"] = 3;
			a["物理学院(三教)"] = 3;
			a["天南大联合研究院（天南门）"] = 3;
			a["南开大学MBA楼"] = 3;
			a["生命科学学院"] = 3;
			a["化学楼"] = 3;
			a["数学学院"] = 3;
			a["外语学院(七教)"] = 3;
			a["东方艺术大楼"] = 3;
			a["思源堂（医学院-二教）"] = 3;
			a["蒙民伟楼"] = 3;
			a["综合实验楼"] = 3;
			a["日本研究院"] = 3;
			a["汉语言学院"] = 3;
			a["西区食堂"] = 4;
			a["职工食堂"] = 4;
			a["三食堂"] = 4;
			a["西南村"] = 4;
			a["8号公寓"] = 5;
			a["4号公寓"] = 5;
			a["3号公寓"] = 5;
			a["2号公寓"] = 5;
			a["1号公寓"] = 5;
			a["7号公寓"] = 5;
			a["6号公寓"] = 5;
			a["博士后公寓"] = 5;
			a["21宿"] = 5;
			a["14宿"] = 5;
			a["15宿"] = 5;
			a["16宿"] = 5;
			a["17宿"] = 5;
			a["18宿"] = 5;
			a["6宿"] = 5;
			a["5宿"] = 5;
			a["13宿"] = 5;
			a["20宿"] = 5;
			a["12宿"] = 5;
			a["8宿"] = 5;
			a["9宿"] = 5;
			a["10宿"] = 5;

			
			ba.clear();
			
			ba.writeShort(arr.length);
			ba.writeShort(0);
			ba.writeShort(0);
			ba.writeShort(0);
			ba.writeShort(0);
			
			for each (var item:BuildingInfo in arr)
			{
				ba.writeShort(item.x);
				ba.writeShort(item.y);
				ba.writeUTF(item.s);
			}
			
			for (var j:int = 0; j < len; j++)
			{
				//注释，这里a[arr[j]._s]便是那个BuildingInfo第j号应该带有的附加信息。
				
				var id:* = a[arr[j]._s];
				ba.writeByte(id==null?6:id);
			}
			
			
//			var dorm:Vector.<BuildingInfo> = arr.filter(
			
			
			return ba;
			
		}

		public static function markerToAssoc(ba:ByteArray):Object
		{
			var len:int = ba.readUnsignedShort();
			var mW:int = ba.readUnsignedShort();
			var mH:int = ba.readUnsignedShort();
			var bW:int = ba.readUnsignedShort();
			var bH:int = ba.readUnsignedShort();
			
			
			var location:Vector.<BuildingInfo> = new Vector.<BuildingInfo>();
			var func:Vector.<BuildingInfo> = new Vector.<BuildingInfo>();
			var familyHouse:Vector.<BuildingInfo> = new Vector.<BuildingInfo>();
			var teachingBuilding:Vector.<BuildingInfo> = new Vector.<BuildingInfo>();
			var canteen:Vector.<BuildingInfo> = new Vector.<BuildingInfo>();
			var dorm:Vector.<BuildingInfo> = new Vector.<BuildingInfo>();
			var others:Vector.<BuildingInfo> = new Vector.<BuildingInfo>();
			
			var varr:Array = new Array(location, func, familyHouse, teachingBuilding, canteen, dorm, others);
			
			var arr:Vector.<BuildingInfo> = new Vector.<BuildingInfo>();
			for (var i:int = 0; i < len; i++)
			{
				arr[i] = new BuildingInfo(ba.readUnsignedShort(),ba.readUnsignedShort(),ba.readUTF());
			}
			for (var j:int = 0; j < len; j++)
			{
				(varr[ba.readByte()] as Vector.<BuildingInfo>).push(arr[j]);
			}
			
			var obj:Object = {};
			obj["地标"] = location;
			obj["功能"] = func;
			obj["家属楼"] = familyHouse;
			obj["教学"] = teachingBuilding;
			obj["食堂"] = canteen;
			obj["宿舍"] = dorm;
			obj["其他"] = others;
			
			return obj;
				
		}
	}
}